<?php


use Phalcon\Mvc\Router;
use Phalcon\DI\FactoryDefault;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Logger\Adapter\File as Logger;

$di = new FactoryDefault();

$loader = new \Phalcon\Loader();
//$loader->registerClasses(
//    array(
//        "Wechat" => __DIR__ . "/../lib/wechat.class.php",
//    )
//);
$loader->registerNamespaces(array(
    'Lib' => __DIR__ . '/../lib/',
));


$loader->register();

$config = include __DIR__ . '/config.php';

$di['config'] = function () use ($config) {
    return $config;
};

$di['router'] = function () {
    $router = new Router();
    $router->setDefaultModule("frontend");
    $router->setDefaultNamespace("Frontend\Controllers");
    $router->add("/admin/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_]*)(/.*)*", array(
        'module' => 'backend',
        "namespace" => "Backend\Controllers",
        "controller" => 1,
        "action" => 2,
        "params" => 3
    ));
    $router->add("/frontend/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_]*)(/.*)*", array(
        'module' => 'frontend',
        "namespace" => "Frontend\Controllers",
        "controller" => 1,
        "action" => 2,
        "params" => 3
    ));
    return $router;
};

$di['url'] = function () {
    $url = new \Phalcon\Mvc\Url();
    return $url;
};

$di['crypt'] = function () use ($config) {
    $crypt = new \Phalcon\Crypt();
    $crypt->setKey($config->crypt->key);
    return $crypt;
};

$di['session'] = function () use ($config) {
    session_save_path($config->application->sessionDir);
    $session = new SessionAdapter();
    $session->start();
    return $session;
};

$di['logger'] = function () use ($config) {
    $logger = new Logger($config->application->logDir);
    return $logger;
};


